package com.flkin.modules.system.service.impl;

import com.flkin.modules.system.domain.Role;
import com.flkin.modules.system.repository.RoleRepository;
import com.flkin.modules.system.service.RoleService;
import com.flkin.modules.system.service.dto.RoleDto;
import com.flkin.modules.system.service.mapstruct.RoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

/**
 * @author yanxiang.huang 2020-12-07 22:31:34
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleRepository roleRepository;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<RoleDto> getAll() {
        return roleMapper.toDto(roleRepository.findAll());
    }

    @Override
    public RoleDto findById(Long id) {
        Role role = roleRepository.findById(id).orElse(null);
        if (role != null) {
            return roleMapper.toDto(role);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoleDto create(Role resource) {
        return roleMapper.toDto(roleRepository.save(resource));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Role resource) {
        roleRepository.save(resource);
    }

    @Override
    public void deleteAll(List<Long> ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public void deleteById(Long id) {
        roleRepository.deleteById(id);
    }

    @Override
    public Set<Role> findByUserId(Long id) {
        return roleRepository.findByUserId(id);
    }
}
